package com.vpx;

import com.gooddies.reflection.ReflectionUtils;
import com.googlecode.javacv.cpp.opencv_core;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;

/**
 * @author sad
 */
abstract class CodecBase {
protected static Field addressField;

protected static synchronized Field getAddressField() {
        if (addressField == null) {
            ReflectionUtils.iterateFields(ByteBuffer.class, new ReflectionUtils.FieldResult() {

                @Override
                public void field(Field f) {
                    if (f.getName().equals("address")) {
                        f.setAccessible(true);
                        addressField = f;
                    }
                }
            });
        }

        return addressField;
    }

    public static long getImageAddress(final opencv_core.IplImage image) {
        try {
            Field addressF = getAddressField();
            return (long) addressF.get(image.getByteBuffer());
        } catch (IllegalAccessException | IllegalArgumentException ex) {
            throw new RuntimeException(ex);
        }
    }
}
